const express = require('express');
const fs = require('fs');
const path = require('path');
const router = express.Router();

// 数据文件路径
const dataPath = path.join(__dirname, '../data/about.json');

// 读取数据的辅助函数
const readData = () => {
  try {
    const data = fs.readFileSync(dataPath, 'utf8');
    return JSON.parse(data);
  } catch (error) {
    console.error('读取关于我们数据文件失败:', error);
    return {};
  }
};

// 写入数据的辅助函数
const writeData = (data) => {
  try {
    fs.writeFileSync(dataPath, JSON.stringify(data, null, 2));
    return true;
  } catch (error) {
    console.error('写入关于我们数据文件失败:', error);
    return false;
  }
};

// 获取公司信息
router.get('/company', (req, res) => {
  try {
    const data = readData();
    res.json({
      success: true,
      data: data.companyInfo || {},
      message: '获取公司信息成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 获取发展历程
router.get('/milestones', (req, res) => {
  try {
    const data = readData();
    res.json({
      success: true,
      data: data.milestones || [],
      message: '获取发展历程成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 获取核心价值
router.get('/values', (req, res) => {
  try {
    const data = readData();
    res.json({
      success: true,
      data: data.coreValues || [],
      message: '获取核心价值成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 获取团队成员
router.get('/team', (req, res) => {
  try {
    const data = readData();
    res.json({
      success: true,
      data: data.teamMembers || [],
      message: '获取团队成员成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 获取联系信息
router.get('/contact', (req, res) => {
  try {
    const data = readData();
    res.json({
      success: true,
      data: data.contactInfo || {},
      message: '获取联系信息成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 获取所有关于我们的数据
router.get('/', (req, res) => {
  try {
    const data = readData();
    res.json({
      success: true,
      data: data,
      message: '获取关于我们数据成功'
    });
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

// 更新公司信息
router.put('/company', (req, res) => {
  try {
    const data = readData();
    const { name, mission, description, features } = req.body;
    
    data.companyInfo = {
      ...data.companyInfo,
      ...(name && { name }),
      ...(mission && { mission }),
      ...(description && { description }),
      ...(features && { features })
    };
    
    if (writeData(data)) {
      res.json({
        success: true,
        data: data.companyInfo,
        message: '更新公司信息成功'
      });
    } else {
      res.status(500).json({
        success: false,
        message: '保存数据失败'
      });
    }
  } catch (error) {
    res.status(500).json({
      success: false,
      message: '服务器内部错误',
      error: error.message
    });
  }
});

module.exports = router;
